<?php

namespace App\Console\Commands;

use App\Models\Game\PlayerGamedata;
use App\Models\Login\Account;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class CommandBase extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'fetch:base';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '基类';
    /**
     * [$yesterday 昨天日期].
     *
     * @var [type]
     */
    protected $yesterday;

    /**
     * [$yesterdayTimestamp 昨天时间戳].
     *
     * @var [type]
     */
    protected $yesterdayTimestamp;

    /**
     * [$today 今天日期].
     *
     * @var [type]
     */
    protected $today;

    /**
     * [$todayTimestamp 今天时间戳].
     *
     * @var [type]
     */
    protected $todayTimestamp;

    /**
     * 统计类型.
     */
    const STAT_TYPE_PLATFORM = 1;
    const STAT_TYPE_CHANNEL = 2;
    const STAT_TYPE_SERVER = 3;
    const STAT_TYPE_SERVER_CHANNEL = 4;
    const DAY_TIMESTAMP = 86400;

    /**
     * Create a new command instance.
     */
    public function __construct()
    {
        parent::__construct();

        $yesterday = Carbon::yesterday();
        $today = Carbon::today();
        $this->yesterday = $yesterday->toDateString();
        $this->yesterdayTimestamp = $yesterday->timestamp;
        $this->today = $today->toDateString();
        $this->todayTimestamp = $today->timestamp;
//        $this->test();
    }

    protected function test()
    {
        setDB(1);
        $old_account = DB::connection('mysql_login')->table('account_test')->selectRaw('ac_account,ac_id')->get();

        $datas = [];
        foreach ($old_account as $old_val) {
            $old_ac = $old_val->ac_account;
            $ac = Account::where('ac_account', $old_ac)->pluck('ac_id')->all();
            if (!$ac) {
                continue;
            }
            $new_userid = $ac[0];

            $db = DB::connection('mysql_game')->table('player_gamedata_prd_1');
            //角色
            $new_name = $db->where('userId', "$new_userid")->pluck('name')->all();
            if (empty($new_name)) {
                continue;
            }
            //老玩家
            $db = DB::connection('mysql_game')->table('player_gamedata');
            $old_name = $db->where('userId', "{$old_val->ac_id}")->pluck('name')->all();

            $names = ['old_name' => $old_name, 'new_name' => $new_name];
            $datas[] = $names;
        }

        dd($datas);

        setDB(2);
        foreach ($ac as $aid) {
            $acids[] = (string) $aid;
        }
        $a = PlayerGamedata::selectRaw('userId,name')->whereIn('userId', $acids)->get()->toArray();
        dd($a);
        foreach ($a as $val) {
            setDB(1);
            $db = DB::reconnect('mysql_game')->table('player_gamedata');
            dd($val['userId']);
            $name = $db->where('userId', "{$val['userId']}")->pluck('name');
            dd($name);
        }
    }
}
